This article mainly describes how to use Magicodes.IE.Csv to import and export Csv files.
Install-Package Magicodes.IE.CsvThrough the following code snippets, we will demonstrate how to export Csv through DTO and corresponding attributes.
ExporterHeaderAttribute
- DisplayName: Display name
- Format: Format string
- IsIgnore: Whether to ignore
public class ExportTestDataWithAttrs
{
[ExporterHeader(DisplayName = "Large Text")]
public string Text { get; set; }
[ExporterHeader(DisplayName = "Normal Text")] public string Text2 { get; set; }
[ExporterHeader(DisplayName = "Ignore", IsIgnore = true)]
public string Text3 { get; set; }
[ExporterHeader(DisplayName = "Number", Format = "#,##0")]
public decimal Number { get; set; }
[ExporterHeader(DisplayName = "Name", IsAutoFit = true)]
public string Name { get; set; }
/// <summary>
/// Time field 1
/// </summary>
[ExporterHeader(DisplayName = "Time1", Format = "yyyy-MM-dd")]
public DateTime Time1 { get; set; }
/// <summary>
/// Time field 2
/// </summary>
[ExporterHeader(DisplayName = "Time2", Format = "yyyy-MM-dd HH:mm:ss")]
public DateTime? Time2 { get; set; }
public DateTime Time3 { get; set; }
public DateTime Time4 { get; set; }
/// <summary>
/// Long value field
/// </summary>
[ExporterHeader(DisplayName = "Long Value", Format = "#,##0")]
public long LongNo { get; set; }
}Export through DTO:
public async Task ExportHeaderAsByteArray_Test()
{
IExporter exporter = new CsvExporter();
var filePath = GetTestFilePath($"{nameof(ExportHeaderAsByteArray_Test)}.csv");
DeleteFile(filePath);
var result = await exporter.ExportHeaderAsByteArray(GenFu.GenFu.New<ExportTestDataWithAttrs>());
}For csv import, we can map to our Dto properties through the ImporterHeader Name attribute. And we can get enumeration types and related mappings through ValueMapping, which can help us convert corresponding values.
public async Task StudentInfoImporter_Test()
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "StudentInfo.csv");
var import = await Importer.Import<ImportStudentDto>(filePath);
} /// <summary>
/// Import student data Dto
/// </summary>
public class ImportStudentDto
{
/// <summary>
/// Serial number
/// </summary>
[ImporterHeader(Name = "Serial Number")]
public long SerialNumber { get; set; }
/// <summary>
/// Student code
/// </summary>
[ImporterHeader(Name = "Student Code")]
public string StudentCode { get; set; }
/// <summary>
/// Name
/// </summary>
[ImporterHeader(Name = "Name")]
public string Name { get; set; }
/// <summary>
/// ID card number
/// </summary>
[ImporterHeader(Name = "ID Card")]
public string IdCard { get; set; }
/// <summary>
/// Gender
/// </summary>
[ImporterHeader(Name = "Gender")]
[ValueMapping("Male", 0)]
[ValueMapping("Female", 1)]
public Genders Gender { get; set; }
/// <summary>
/// Home address
/// </summary>
[ImporterHeader(Name = "Home Address")]
public string Address { get; set; }
/// <summary>
/// Guardian name
/// </summary>
[ImporterHeader(Name = "Guardian Name")]
public string Guardian { get; set; }
/// <summary>
/// Guardian phone
/// </summary>
[ImporterHeader(Name = "Guardian Phone")]
public string GuardianPhone { get; set; }
/// <summary>
/// Student number
/// </summary>
[ImporterHeader(Name = "Student Number")]
public string StudentNub { get; set; }
/// <summary>
/// Dormitory number
/// </summary>
[ImporterHeader(Name = "Dormitory Number")]
public string DormitoryNo { get; set; }
/// <summary>
/// QQ
/// </summary>
[ImporterHeader(Name = "QQ Number")]
public string QQ { get; set; }
/// <summary>
/// Nation
/// </summary>
[ImporterHeader(Name = "Nation")]
public string Nation { get; set; }
/// <summary>
/// Household type
/// </summary>
[ImporterHeader(Name = "Household Type")]
public string HouseholdType { get; set; }
/// <summary>
/// Phone
/// </summary>
[ImporterHeader(Name = "Student Phone")]
public string Phone { get; set; }
/// <summary>
/// Status
/// Can be empty enumeration
/// </summary>
[ImporterHeader(Name = "Status")]
public StudentStatus? Status { get; set; }
/// <summary>
/// Remark
/// </summary>
[ImporterHeader(Name = "Remark")]
public string Remark { get; set; }
/// <summary>
/// Whether boarding (ignore)
/// </summary>
[ImporterHeader(IsIgnore = true)]
public bool? IsBoarding { get; set; }
/// <summary>
/// Class id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid ClassId { get; set; }
/// <summary>
/// School Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? SchoolId { get; set; }
/// <summary>
/// Campus Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? CampusId { get; set; }
/// <summary>
/// Major Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? MajorsId { get; set; }
/// <summary>
/// Grade Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? GradeId { get; set; }
}